// 导入getHomeGoodPriceData函数
import { getHomeDiscountData, getHomeGoodPriceData, getHomeHighScoreData, getHomeHotRecommendData, getHomeLongForData, getHomePlusData } from '@/services'
// 导入createSlice和createAsyncThunk函数
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'

// 创建fetchHomeDataAction异步函数
export const fetchHomeDataAction = createAsyncThunk("fetchData", (payload, { dispatch }) => {
  // 获取高性价比房源信息
  getHomeGoodPriceData().then((res) => {
    dispatch(changeGoodPriceInfoAction(res))
  })
  // 获取高评分房源信息
  getHomeHighScoreData().then((res) => {
    dispatch(changeHigthScoreInfoAction(res))
  })

  // 获取高折扣房源信息
  getHomeDiscountData().then((res) => {
    dispatch(changeDiscountInfoAction(res))
  })

  // 获取热门推荐信息
  getHomeHotRecommendData().then((res) => {
    dispatch(changeRecommendInfoAction(res))
  })

  // 获取猜你想去信息
  getHomeLongForData().then((res) => {
    dispatch(changeLongForInfoAction(res))
  })

  // 获取plus信息
  getHomePlusData().then((res) => {
    dispatch(changePlusInfoAction(res))
  })
})



// 创建homeSlice状态层
const homeSlice = createSlice({
  // 状态层名称
  name: "home",
  // 初始状态
  initialState: {
    // 高性价比房源数据
    goodPriceInfo: {
    },
    // 高评分房源数据
    higthScoreInfo: {},

    // 高折扣房源数据
    discountInfo: {},

    // 热门推荐数据
    recommendInfo: {},

    // 猜你想去数据 
    longForInfo: {},

    // plus数据
    plusInfo: {},
  },

  // 计算属性
  reducers: {
    // 修改高性价比房源数据
    changeGoodPriceInfoAction(state, { payload }) {
      state.goodPriceInfo = payload
    },
    // 修改高评分房源数据
    changeHigthScoreInfoAction(state, { payload }) {
      state.higthScoreInfo = payload
    },
    // 修改高折扣房源数据
    changeDiscountInfoAction(state, { payload }) {
      state.discountInfo = payload
    },
    // 修改热门推荐数据
    changeRecommendInfoAction(state, { payload }) {
      state.recommendInfo = payload
    },
    // 修改猜你想去数据
    changeLongForInfoAction(state, { payload }) {
      state.longForInfo = payload
    },

    // 修改plus数据
    changePlusInfoAction(state, { payload }) {
      state.plusInfo = payload
    }
  },
  // extraReducers: {
  //   [fetchHomeDataAction.fulfilled](state, { payload }) {
  //     console.log(payload)
  //     state.goodPriceInfo = payload
  //   }
  // }
})

// 导出action
export const {
  changeGoodPriceInfoAction,
  changeHigthScoreInfoAction,
  changeDiscountInfoAction,
  changeRecommendInfoAction,
  changeLongForInfoAction,
  changePlusInfoAction
} = homeSlice.actions

// 默认导出状态层reducer
export default homeSlice.reducer
